package com.huazai.thread.mine;

import lombok.extern.slf4j.Slf4j;

import java.util.Scanner;

/**
 * @author Seon
 * @date 2022/8/28 2:50
 */
@Slf4j
public class SeonRedemption {
    public static void main(String[] args) throws InterruptedException {

        Scanner sc=new Scanner(System.in);

        System.out.println("请输入票数：");

        int num=sc.nextInt();        //从键盘读入总票数
        Ticket tickets = new Ticket(num);    //产生票

        new Thread(new sellTicketThread(tickets)).start();//sellTicketThread售票
        new Thread(new returnTicketThread(tickets)).start();//returnTicketThread退票

        Thread.sleep(50);    //休眠等待售票和退票执行完毕
        System.out.println(tickets.freeNum);
    }
}


class Ticket {
    int total;        //总票数
    int freeNum;    //多线程共享变量：余票数量
    int soldNum;    //已售出票数
    boolean hasTicket;    //true表示有足够的票出售，false则表示票数不够

    int count = 3;    //线程售票退票次数
    int sellNum = 3;    //单次售票数量
    int returnNum = 2;    //单次退票数量

    public Ticket(int number) {
        total = number;
        freeNum = total;    //售票前：总数与余票数相等
        soldNum = 0;            //已售出票数
        hasTicket = (freeNum >= sellNum);    //余票足够
    }
    //在下面补充Ticket类的synchronized售票方法
    synchronized public void sellTicket(){    //num表示购票数，也就是售出票数
        while (freeNum < sellNum) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        freeNum -= sellNum;
        soldNum += sellNum;
        System.out.println("售出" + sellNum + "余票" + freeNum);
        this.notifyAll();
    }

    //在下面补充Ticket类的synchronized退票方法，并将Ticket类补充完整
    synchronized public void returnTicket() {   //num表示退票数
        while (soldNum < returnNum || freeNum >= sellNum) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        freeNum += returnNum;
        soldNum -= returnNum;
        System.out.println("退回" + returnNum + "余票" + freeNum);
        this.notifyAll();
    }

}


//在下面补充SellTicketThread类，通过实现Runnable实现SellTicketThread类
class sellTicketThread implements Runnable{
    Ticket preTic;

    public sellTicketThread(Ticket preTic) {
        this.preTic = preTic;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            preTic.sellTicket();
        }
    }

}

//在下面补充ReturnTicketThread类，通过实现Runnable实现ReturnTicketThread类
class returnTicketThread implements Runnable{
    Ticket preTic;

    public returnTicketThread(Ticket preTic) {
        this.preTic = preTic;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            preTic.returnTicket();
        }
    }
}
